package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.AreaListAdapter;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.view.AreaSelectView;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

/**
 * Created by zw on 2016/3/2 0002.
 * 商户地区
 */
public class MerchantAreaActivity extends ABBaseActivity {

    private TitleBar titleBar;
    private TextView tv_addArea;
    private ListView lv_areaList;
    private AreaSelectView areaPopView;
    private AreaListAdapter adapter;

    private boolean isSingle = false;//是否是单选

    @Override
    protected int getLayoutId() {
        return R.layout.activity_merchant_area;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
        titleBar.setShowRight(View.VISIBLE);
        titleBar.setRightTextView("确定");
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        tv_addArea = getViewById(R.id.tv_add_area);
        lv_areaList = getViewById(R.id.lv_area_list);
        adapter = new AreaListAdapter(mContext);
        lv_areaList.setAdapter(adapter);

        areaPopView = new AreaSelectView(mContext);

        if (bundle != null) {
            isSingle = bundle.getBoolean(Constant.IS_SINGLE);
        }
    }

    @Override
    protected void eventOnClick() {
        //确定
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                if (adapter.list != null && adapter.list.size() > 0) {
                    StringBuffer areaStr = new StringBuffer();
                    for (String str : adapter.list) {
                        areaStr.append(str + ",");
                    }
                    Intent intent = new Intent();
                    intent.putExtra("area", areaStr.toString().substring(0, areaStr.toString().length() - 1));
                    setResult(RESULT_OK, intent);
                }
                finish();
            }
        });

        tv_addArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWindow();
            }
        });
        //选择地区完成回调
        areaPopView.setOnAreaSelectedListener(new AreaSelectView.OnAreaSelectedListener() {
            @Override
            public void onSelected(String area) {
                if (!adapter.list.contains(area)) {
                    if (isSingle) {
                        adapter.removeAll();
                    }
                    adapter.add(area);
                    areaPopView.dismiss();
                } else {
                    showToast("已存在");
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //添加地区
                showPopWindow();
            }
        },100);

    }

    private void showPopWindow() {
        if (areaPopView == null) {
            areaPopView = new AreaSelectView(getParent());
        }
//        WindowManager.LayoutParams params = getWindow().getAttributes();
//        params.alpha = 0.7f;
//        getWindow().setAttributes(params);
        if (!isFinishing())//add by zhuangzeqin 2017年5月2日11:42:25  修复偶发奔溃的bug
            areaPopView.showAtLocation(lv_areaList, Gravity.BOTTOM, 0, 0);


    }
}
